
const {ccclass, property} = cc._decorator;

@ccclass
export default class birdFlyControl extends cc.Component {


    // LIFE-CYCLE CALLBACKS:
    // 飞行速度
    speed: number = 70;
    // 小鸟血量
    hp: number = 1;

    // 小鸟击落回调
    dieCallBack: Function;
    // 分数增加回调
    addScoreCallBack: Function;

    // onLoad () {}

    start () {
        
    }

    fly() {
        // 随机的x
        let targetPos = cc.v2(Math.random() * 278 - 139, 110);
        let currentPos = this.node.getPosition();
        let distance = currentPos.sub(targetPos).mag();
        
        // 转换方向
        if (targetPos.x > currentPos.x) {
            this.node.scaleX = -1;
        }
        
        let action = cc.moveTo(distance / this.speed, targetPos);

        let seq = cc.sequence(action, cc.callFunc(()=>{
            // console.log('动作都执行完了, 没有被击落');
            this.dieCallBack();
        }));
        this.node.runAction(seq);

        // 飞出屏幕

        // 触摸到小鸟
        this.node.on(cc.Node.EventType.TOUCH_START, (event)=>{
            if (this.hp > 0) {
                this.hp --;
                // 停止飞行方向
                this.node.stopAction(seq);
                // 修改飞行动画
                let anim = this.getComponent(cc.Animation);
                anim.play('birddie');
                
                // 向下坠落
                currentPos = this.node.getPosition();
                let moveDie = cc.moveTo((currentPos.y + 80) / (this.speed * 2), cc.v2(currentPos.x, -80));
                this.node.runAction(cc.sequence(moveDie, cc.callFunc(()=>{
                    // 小鸟销毁
                    this.node.destroy();
                })));
                this.addScoreCallBack();
            }
            
        })
    }

    // update (dt) {}
}
